#if !MONODROID
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Reflection;

namespace Devsoft.Utilities
{    
    public class Language
    {
        #region Property FileName
        protected string _FileName;
        public string FileName
        {
            get
            {
                return _FileName;
            }
            set
            {
                _FileName = value;
            }
        }
        #endregion

        #region Property Language
        protected string _Language;
        public string LanguageName
        {
        	get
        	{
        		return _Language;
        	}
        	set
        	{
        		_Language = value;
        	}
        }
        #endregion

        #region Property Instance
        protected static Language _Instance = null;
        public static Language Instance
        {
        	get
        	{
        		if(_Instance == null)
        		{
        			_Instance = new Language();
        		}
        		return _Instance;
        	}
        }
        #endregion



        protected XmlDocument _XmlDoc;
        protected XmlNode _XmlLanguage;

        public Language()
        {
            Initialize();
        }
        
        virtual protected void Initialize()
        {
            _FileName = "xml/languages.xml";
            _Language = "vi";
        }

        virtual public void Load(string sFileName)
        {
            _FileName = sFileName;
            Load();
        }

        virtual public bool Load()
        {
            _XmlDoc = new XmlDocument();
            _XmlDoc.Load(_FileName);
            _XmlLanguage = _XmlDoc.SelectSingleNode(string.Format("/Languages/Language[@name=\"{0}\"]",_Language));
            return _XmlLanguage != null;
        }

        virtual public void CreateLanguage(string sName,object obj)
        {
            XmlNode xNode =  _XmlLanguage.SelectSingleNode(sName);
            while (xNode != null)
            {
                XmlNode pNode = xNode;
                xNode = pNode.NextSibling;
                _XmlLanguage.RemoveChild(pNode);
            }//End While
            xNode = WalkThroughControls(sName, obj);
            _XmlLanguage.AppendChild(xNode);
            _XmlDoc.Save(_FileName);
        }

        virtual protected XmlNode WalkThroughControls(string sName,object obj)
        {
            XmlNode xRootNode = _XmlDoc.CreateElement(sName);
            System.Type thisType = obj.GetType();
            
            FieldInfo[] thisFields = thisType.GetFields(BindingFlags.DeclaredOnly |
                        BindingFlags.Public | BindingFlags.NonPublic |
                        BindingFlags.Instance);

            foreach (FieldInfo f in thisFields)
            {
                XmlNode xNode = _XmlDoc.CreateElement(f.Name);

                object objField = f.GetValue(obj);

                if (objField is System.Windows.Forms.Label)
                {
                    System.Windows.Forms.Label lb = objField as System.Windows.Forms.Label;
                    xNode.InnerText = lb.Text;
                }//End If

                else if (objField is System.Windows.Forms.Button)
                {
                    System.Windows.Forms.Button lb = objField as System.Windows.Forms.Button;
                    xNode.InnerText = lb.Text;
                }//End If

                else if (objField is System.Windows.Forms.Control)
                {
                    System.Windows.Forms.Control lb = objField as System.Windows.Forms.Control;
                    if (lb.Controls.Count > 0)
                    {
                        xNode = WalkThroughControls(f.Name, objField);
                    }//End If

                    
                }//End If

                xRootNode.AppendChild(xNode);
            }
            /*
            foreach (System.Windows.Forms.Control c in controls)
            {
                XmlNode xNode = _XmlDoc.CreateElement(c.Name);
                if (c is System.Windows.Forms.Label)
                {
                    System.Windows.Forms.Label lb = c as System.Windows.Forms.Label;                   
                    xNode.InnerText = xn
                }//End If
   
                if (c.Controls.Count > 0)
                {
                    SetXpStyle(c.Controls);
                }

                xRootNode.AppendChild(xNode);

            }//End Foreach
             * */

            return xRootNode;
        }

    }
}
#endif